package ai.cast.reconnaissance.api;

import org.springframework.http.ResponseEntity;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;


/**
 * 分析当前盘面数据，给出建议
 */
@RestController
@RequestMapping(value = "/api/analysis")
public class AnalysisApi {

    @RequestMapping("/red_situation")
    public String redSituation(@RequestParam(value = "simulationId") String simulationId) {
        return "{\"red_situation\":\"red_situation\"}";
    }

    @RequestMapping("/blue_situation")
    public String blueSituation(@RequestParam(value = "simulationId") String simulationId) {
        return "{\"blue_situation\":\"blue_situation\"}";
    }

    @RequestMapping("/task")
    public String taskSituation(@RequestParam(value = "simulationId") String simulationId) {
        return """
            {
                "totalTasks": 12,
                "completedTasks": 9,
                "inProgressTasks": 2,
                "failedTasks": 1,
                "taskExecutionStatus": [
                  {
                    "taskId": "task-001",
                    "status": "completed",
                    "executionTime": "2025-09-11T10:15:00Z",
                    "description": "侦察任务1，已完成，目标覆盖率80%"
                  },
                  {
                    "taskId": "task-002",
                    "status": "inProgress",
                    "executionTime": "2025-09-11T10:30:00Z",
                    "description": "侦察任务2，进行中，目标识别准确率70%"
                  },
                  {
                    "taskId": "task-003",
                    "status": "failed",
                    "executionTime": "2025-09-11T11:00:00Z",
                    "description": "侦察任务3，失败，无法识别目标"
                  }
                ]
              }
            """;
    }

    @RequestMapping("/difficulty")
    public ResponseEntity<String> difficulty(@RequestParam(value = "simulationId") String simulationId) {
        String fileName = getFileName(simulationId);

        try {
            Path filePath = Paths.get(ResourceUtils.getURL("classpath:difficulty/" + fileName).toURI());
            String content = new String(Files.readAllBytes(filePath));
            return ResponseEntity.ok(content);
        } catch (Exception e) {
            return ResponseEntity.status(500).body("Error reading the file: " + e.getMessage());
        }
    }

    private static String getFileName(String simulationId) {
        String fileName;

        simulationId = simulationId.substring(0,6);
        switch (simulationId) {
            case "case01":
                fileName = "case1.json";
                break;
            case "case02":
                fileName = "case2.json";
                break;
            case "case03":
                fileName = "case3.json";
                break;
            default:
                throw new RuntimeException();
        }
        return fileName;
    }

    /**
     * 红方侦察表现，通过 覆盖率、重访率、识别正确率和加权总分表示
     */
    @RequestMapping("/red_capability")
    public ResponseEntity<String> redCapability(@RequestParam(value = "simulationId") String simulationId) {
        String fileName = getFileName(simulationId);

        try {
            Path filePath = Paths.get(ResourceUtils.getURL("classpath:red_behaviour/" + fileName).toURI());
            String content = new String(Files.readAllBytes(filePath));
            return ResponseEntity.ok(content);
        } catch (Exception e) {
            return ResponseEntity.status(500).body("Error reading the file: " + e.getMessage());
        }
    }

    /*
     同类型侦察仿真任务的较好表现数据
     */
    @RequestMapping("/red_historical_behavior")
    public ResponseEntity<String> redHistoricalBehavior(@RequestParam(value = "simulationId") String simulationId) {
        String fileName = getFileName(simulationId);
        try {
            Path filePath = Paths.get(ResourceUtils.getURL("classpath:historical_behaviour/" + fileName).toURI());
            String content = new String(Files.readAllBytes(filePath));
            return ResponseEntity.ok(content);
        } catch (Exception e) {
            return ResponseEntity.status(500).body("Error reading the file: " + e.getMessage());
        }
    }

    @RequestMapping("/suggestion")
    public String memoryData(@RequestParam(value = "simulationId") String simulationId) {
        return "{\"suggestion\":\"analysis\"}";
    }


}
